package com.czxy.user.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.czxy.domain.TbCandidates;
import com.czxy.domain.TbPhoto;
import com.czxy.domain.TbUser;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.czxy.findjob.vo.BaseResult;
import com.czxy.user.feign.CandidiatesFeign;
import com.czxy.user.feign.EnterpriseFeign;
import com.czxy.user.feign.PhotoFeign;
import com.czxy.user.mapper.UserMapper;
import com.czxy.user.service.UserService;
import com.czxy.vo.ConditionUser;
import com.czxy.vo.MonthVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author xuewen
 * @since 2022-02-06
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, TbUser> implements UserService {

    @Autowired
    private CandidiatesFeign candidiatesFeign;
    @Autowired
    private PhotoFeign photoFeign;
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Resource
    private EnterpriseFeign enterpriseFeign;
    @Override
    public TbUser login(TbUser tbUser) {

        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("phone",tbUser.getPhone());
        queryWrapper.eq("password",tbUser.getPassword());
        TbUser user = this.baseMapper.selectOne(queryWrapper);
        if (user!=null){
            BaseResult<TbCandidates> candidatesBaseResult = candidiatesFeign.getById(user.getCandidatesId());
            TbCandidates data = candidatesBaseResult.getData();
            user.setTbCandidates(data);
            Integer photoId = user.getPhotoId();
            BaseResult<TbPhoto> photoBaseResult = photoFeign.findById(String.valueOf(photoId));
            user.setPhoto(photoBaseResult.getData());
            return user;
        }else {
            return null;
        }
    }

    @Override
    public Page condition(ConditionUser conditionUser) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.like(StringUtils.isNotBlank(conditionUser.getUsername()),"username",conditionUser.getUsername());
        queryWrapper.like(StringUtils.isNotBlank(conditionUser.getPhone()),"phone",conditionUser.getPhone());
        queryWrapper.eq(StringUtils.isNotBlank(conditionUser.getLevel()),"level",conditionUser.getLevel());
        queryWrapper.ge(StringUtils.isNotBlank(conditionUser.getMinCreateTime()),"create_time",conditionUser.getMinCreateTime());
        queryWrapper.le(StringUtils.isNotBlank(conditionUser.getMaxCreateTime()),"create_time",conditionUser.getMaxCreateTime());
        Page page = new Page(conditionUser.getCurrent(),conditionUser.getSize());
        this.baseMapper.selectPage(page,queryWrapper);
        return page;
    }

    @Override
    public void register(TbUser tbUser) {
        String code = stringRedisTemplate.opsForValue().get(tbUser.getPhone());
        if (!code.equalsIgnoreCase(tbUser.getCode())){
            throw new RuntimeException("验证码错误");
        }
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("phone",tbUser.getPhone());
        List list = this.baseMapper.selectList(queryWrapper);
        if (list.size()!=0){
            throw new RuntimeException("当前号码已注册");
        }
        TbCandidates tbCandidates = new TbCandidates();
        tbCandidates.setName(tbUser.getPhone());
        tbCandidates.setPhone(tbUser.getPhone());
        tbCandidates.setOccupationId(2);
        Integer id = candidiatesFeign.save(tbCandidates).getData();
        tbUser.setLevel(1);
        tbUser.setPhotoId(27);
        tbUser.setState(1);
        tbUser.setCreateTime(new Date());
        tbUser.setUsername(tbUser.getPhone());
        tbUser.setCandidatesId(id);
        this.baseMapper.insert(tbUser);
    }

    @Override
    public boolean reSetPassword(TbUser tbUser) {
        String code = stringRedisTemplate.opsForValue().get(tbUser.getPhone());
        if (!code.equalsIgnoreCase(tbUser.getCode())){
            throw new RuntimeException("验证码错误");
        }
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("phone",tbUser.getPhone());
        List<TbUser> list = this.baseMapper.selectList(queryWrapper);
        TbUser user = list.get(0);
        user.setPassword(tbUser.getPassword());
        this.baseMapper.updateById(user);
        return true;
    }

    @Override
    public MonthVo findCountByDate() {
        MonthVo monthVo = new MonthVo();
        monthVo.setEnterpriseCount(enterpriseFeign.findCount().getData());
        monthVo.setUserCount(this.baseMapper.selectList(null).size());
        int thisMonth = new Date().getMonth() + 1;
        List<String> list = new ArrayList<>();
        list.add(thisMonth+"月");
        Integer num = 13;
        for (int i = 1; i <= 4 ; i++) {
            if (thisMonth-i<=0){
                list.add((num-=1)+"月");
            }else {
                list.add(thisMonth-i+"月");
            }
        }
        Collections.reverse(list);
        monthVo.setMonth(list);
        List<Integer> numList = new ArrayList<>();
        numList.add(this.baseMapper.findCountByDate(4));
        numList.add(this.baseMapper.findCountByDate(3));
        numList.add(this.baseMapper.findCountByDate(2));
        numList.add(this.baseMapper.findCountByDate(1));
        numList.add(this.baseMapper.findCountByDate(0));
        monthVo.setNum(numList);
        return monthVo;
    }
}
